<?php
namespace App\Http\Controllers\API\Wechat;

use App\Models\ScanLog;
use App\Repositories\UsersRepository;
use BaconQrCode\Encoder\QrCode;
use Illuminate\Http\Request;

class CenterController extends \App\http\Controllers\Home\BaseController {

    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        $userRep = app()->build(UsersRepository::class);
        $userService = $this->userService;
        $shop = $this->shop;
//        $wechatlog = D('Home/WechatLog','Logic');
        $this->server->setMessageHandler(function ($message) use($userService, $userRep, $shop) {
            $shop_id = $shop->id;
            $openId = $message->FromUserName; // 用户的openid
            if ($message->MsgType == 'event') {
                # code...
                switch ($message->Event) {
                    case 'subscribe':
                        if($openId){
                            $userinfo = $userRep->checkHasAndSaveInfoByWechat($userService->get($openId),$shop_id);
                        }
                        if (isset($message->EventKey)){
                            $key = str_replace('qrscene_','',$message->EventKey);
                            $qrcode = \App\Models\Qrcode::where([
                                ['url',$key],
                                ['shop_id',$shop_id],
                            ])->first();

                            ScanLog::create([
                                'shop_id'=>$shop_id,
                                'user_id'=>$userinfo->id,
                                'qrcode_id'=>$qrcode->id,
                                'env'=>json_encode($GLOBALS['_SERVER']),
                                'created_at'=>date('Y-m-d H;i:s')
                            ]);
                            $qrcode->scan_number ++;
                            $qrcode->save();
                        }
                        if($shop->wechat->subscribe_reply)
                            return $shop->wechat->subscribe_reply;
                        else
                            return "您好！欢迎关注".$shop->name."!";
                        break;

                    default:
                        # code...
                        break;
                }
            }
            switch ($message->MsgType) {
                case 'text':
//                    return '收到文字消息';
                    break;
                case 'image':
//                    return '收到图片消息';
                    break;
                case 'voice':
//                    return '收到语音消息';
                    break;
                case 'video':
//                    return '收到视频消息';
                    break;
                case 'location':
//                    return '收到坐标消息';
                    break;
                case 'link':
//                    return '收到链接消息';
                    break;
                // ... 其它消息
                default:
//                    return '收到其它消息';
                    break;
            }
            //不返回
            return null;
        });
        $response = $this->server->serve();
        $response->send();
    }

    public function jsConfig(Request $request)
    {
        $js = $this->app->js;
        $request_url = request('request_url');
        if($request_url){
            $js->setUrl($request_url);
        }
        $config = $js->config(array(
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'onMenuShareQQ',
            'onMenuShareWeibo',
            'onMenuShareQZone',
            'chooseImage',
            'previewImage',
            'uploadImage',
            'downloadImage',
            'hideMenuItems',
            'closeWindow',
            'showMenuItems',
            'hideAllNonBaseMenuItem',
            'showAllNonBaseMenuItem',
            'chooseWXPay',
            'openAddress',
        ), false);
        return responseZK(1, $config);
    }

    public function getAccessToken()
    {
        $accessToken = $this->app->access_token; // EasyWeChat\Core\AccessToken 实例
        $token = $accessToken->getToken(); // token 字符串
        return responseZK(1, $token);
    }

    public function setMenu()
    {
        $menu = $this->app->menu;
        $buttons = [
            [
                "type" => "view",
                "name" => "菜场",
                "url"  => "https://mall.hellogreens.cn/web_index/index.html"
            ],
            [
                "type" => "miniprogram",
                // 菜单名称
                "name" => "商城",
                // 对应小程序的appid
                "appid" =>  "wxdfab6fc33d9b5230",
                // 小程序路径
                "pagepath" =>  "pages/loading/loading",
                // 备用网页url
                "url" =>  "https://mall.hellogreens.cn/share",
            ]

        ];
        dump($buttons);
        dump($menu->add($buttons));
    }
}